<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class price_set extends Page
{
       public $cmbType = null;
       public $editPrice = null;
       public $editBrand = null;
       public $btnSet = null;
       public $Label3 = null;
       public $Label2 = null;
       public $Label1 = null;
       function editPriceJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //accept only numbers
       var numero = document.getElementById("editPrice").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("editPrice").value = numero;
       <?php

       }

       function btnSetClick($sender, $params)
       {
        $cmb_ii =  $this->cmbType->getItemIndex();
        if($cmb_ii!='Select Fuel Type')
        {
        $query_count = "SELECT auto_id FROM fuel_price_tbl WHERE brand='".$this->editBrand->Text."' AND type='".$this->cmbType->getItemIndex()."' AND company_id='".$_SESSION['company_id']."'";
        $result_count = mysql_query($query_count) or die("SELECT FUEL PRICE ".mysql_error());
        $count = mysql_num_rows($result_count);

        if($count!=0)
        {
         $row_count = mysql_fetch_array($result_count);
         $auto_id = $row_count['auto_id'];

         $query_up_price = "UPDATE fuel_price_tbl SET price='".$this->editPrice->Text."' WHERE auto_id='".$auto_id."'";
         mysql_query($query_up_price) or die("UPDATE PRICE ".mysql_error());
        }
        if($count==0)
        {
         $query_ins_price = "INSERT INTO fuel_price_tbl (company_id, brand, type, price) VALUES
         ('".$_SESSION['company_id']."','".$this->editBrand->Text."','".$this->cmbType->getItemIndex()."','".$this->editPrice->Text."')";
         mysql_query($query_ins_price) or die("INSERT FUEL PRICE ".mysql_error());
        }
        }
        if($cmb_ii=='Select Fuel Type')
        {
         echo "<script type=text/javascript>
         alert('Please select fuel type');
         </script>";
        }
       }

       function cmbTypeChange()
       {
        $query_price = "SELECT price FROM fuel_price_tbl WHERE brand='".$this->editBrand->Text."' AND type='".$this->cmbType->getItemIndex()."' AND company_id='".$_SESSION['company_id']."'";
        $result_price = mysql_query($query_price) or die("SELECT FUEL PRICE ".mysql_error());
        $row_price = mysql_fetch_array($result_price);

        $this->editPrice->setText($row_price['price']);
       }

       function cmbTypeJSChange($sender, $params)
       {
       echo $this->cmbType->ajaxCall('cmbTypeChange');
       ?>
       return false;
       <?php

       }


       function price_setCreate($sender, $params)
       {
       $this->editBrand->setText(base64_decode($_GET['brand_name']));
       $this->editPrice->setText('');
       $query_type = "SELECT value FROM lookup_fuel_tbl WHERE category='octane/types' AND company_id='".$_SESSION['company_id']."' ORDER BY value ASC";
       $result_type = mysql_query($query_type) or die("SELECT FUEL TYPE ".mysql_error());
       $this->cmbType->Clear();
       $this->cmbType->setItemIndex('Select Fuel Type');
       $this->cmbType->AddItem('Select Fuel Type','','Select Fuel Type');
       while($row_type = mysql_fetch_array($result_type))
       {
        $value = $row_type['value'];
        $this->cmbType->AddItem($value,null,$value);
       }
       }

}

global $application;

global $price_set;

//Creates the form
$price_set=new price_set($application);

//Read from resource file
$price_set->loadResource(__FILE__);

//Shows the form
$price_set->show();

?>